import { message } from 'antd'
import { uniqBy } from 'lodash';
import {
  getOrganizaInsider,// 获取组织所有部门
  getInsiderUserList,// 获取人员
  setUserRole,// 设置人员权限
  addOrganizaInsider,// 新增部门
  editOrganizaInsider,// 编辑部门
  searchOrganizaInsider,// 根据ID查询部门
  deleteOrganizaInsider,// 删除部门
  changeUserDeptRel,// 更换人员所在部门
  deleteDeptUserRel,// 根据用户id和部门id删除用户和部门关系
  getRolesByOrgId, // 根据ORG查询角色权限列表
  inviteUserJoinOrg,//邀请用户加入组织 or 组织下的部门
  fuzzySearchUserList,// 模糊查询用户
} from '@/services/organizationApi';
// import { getURLparams } from '@/utils/utils'

export default {
  namespace: 'orgInsider',
  state: {
    orgDepartmentList: [],
    orgDepartmentInfo: {},
    orgDepartmentUser: {},
    rolePromiseList: []
  },

  effects: {
    * getOrganizaInsider({ payload }, { call, put }) {//获取组织下的部门
      const { resolve } = payload;
      const { code, msg, data } = yield call(getOrganizaInsider, payload);
      !!resolve && resolve(code);
      // console.log(`getOrganizaInsider------------>`, msg, data)
      if (code === 1 && data) {
        yield put({
          type: 'updateState',
          payload: {
            orgDepartmentList: data,
          },
        });
      } else {
        message.error(msg)
      }
    },
    * getInsiderUserList({ payload }, { call, put }) {//获取部门下的人员
      const { code, msg, data } = yield call(getInsiderUserList, payload);
      // console.log(`无所属部门人员返回的数据------------>`, code, msg, data, payload)
      if (code === 1) {
        if (data) {
          yield put({
            type: 'updateState',
            payload: {
              orgDepartmentUser: data
            },
          });
          yield put({
            type: 'userOrdepartment',
            payload: {
              data: data,
              userId: payload
            },
          });
        }
      } else {
        message.error(msg)
      }
    },
    * setUserRole({ payload }, { call, put }) {//设置人员权限
      const { resolve } = payload;
      const { code, msg, data } = yield call(setUserRole, payload);
      !!resolve && resolve(code);
      if (code === 1) {
        message.success(msg)
      } else {
        message.error(msg)
      }
    },
    * searchOrganizaInsider({ payload }, { call, put }) {//根据Id查询组织下的部门
      const { code, msg, data } = yield call(searchOrganizaInsider, payload);
      // console.log(`searchOrganizaInsider------------>`, msg, data)
      if (code === 1 && data) {
        yield put({
          type: 'updateState',
          payload: {
            orgDepartmentInfo: data
          },
        });
      } else {
        message.error(msg)
      }
    },
    * addOrganizaInsider({ payload }, { call, put }) {//新增部门
      const { resolve } = payload;
      const { code, msg, data } = yield call(addOrganizaInsider, payload);
      !!resolve && resolve(code);// 通过resolve返回数据，返回的数据将在Promise的then函数中获取到
      if (code === 1) {
        message.success(msg)
      } else {
        message.error(msg)
      }
    },
    * editOrganizaInsider({ payload }, { call, put }) {//编辑部门信息
      const { resolve } = payload;
      const { code, msg, data } = yield call(editOrganizaInsider, payload);
      !!resolve && resolve(code);// 通过resolve返回数据，返回的数据将在Promise的then函数中获取到
      if (code === 1) {
        message.success(msg)
      } else {
        message.error(msg)
      }
    },
    * deleteOrganizaInsider({ payload }, { call, put }) {//删除部门信息
      const { resolve } = payload;
      const { code, msg, data } = yield call(deleteOrganizaInsider, payload);
      !!resolve && resolve(code);
      if (code === 1) {
        message.success(msg)
      } else {
        message.error(msg)
      }
    },
    * changeUserDeptRel({ payload }, { call, put }) {//更改用户所属部门
      const { resolve } = payload;
      const { code, msg, data } = yield call(changeUserDeptRel, payload);
      !!resolve && resolve(code);
      if (code === 1) {
        message.success(msg)
      } else {
        message.error(msg)
      }
    },
    * deleteDeptUserRel({ payload }, { call, put }) {//根据用户id和部门id删除用户和部门关系
      const { resolve } = payload;
      const { code, msg, data } = yield call(deleteDeptUserRel, payload);
      !!resolve && resolve(code);
      if (code === 1) {
        message.success(msg)
      } else {
        message.error(msg)
      }
    },
    * getRolesByOrgId({ payload }, { call, put }) {//获取人员权限列表
      const { code, msg, data } = yield call(getRolesByOrgId, payload);
      // console.log(`人员权限列表------------>`, code, msg, data)
      if (code === 1) {
        yield put({
          type: 'updateState',
          payload: {
            rolePromiseList: data
          },
        });
      } else {
        message.error(msg)
      }
    },
    * inviteUserJoinOrg({ payload }, { call, put }) {//邀请用户加入组织 or 组织下的部门
      const { resolve } = payload;
      const { code, msg, data } = yield call(inviteUserJoinOrg, payload);
      !!resolve && resolve(code);
      if (code === 1) {
        message.success(msg)
      } else {
        message.error(msg)
      }
    },

  },

  reducers: {
    updateState(state, { payload }) {
      return {
        ...state,
        ...payload,
      }
    },
    userOrdepartment(state, { payload }) {
      if (payload.userId.deptId === null) {
        payload.data.map(item => {
          item["group"] = null;
          item["pathName"] = null
          item["uid"] = item.id
          item.id = `user-${item.id}`
        })
        state.orgDepartmentList = uniqBy(state.orgDepartmentList.concat(payload.data))
      } else {
        findOut(state.orgDepartmentList, payload);
      }
    },
  },
};

const findOut = (list, payload) => {
  list.forEach(item => {
    if (item.children && item.id !== payload.userId.deptId) {
      return findOut(item.children, payload);
    }
    if (item.children) {
      item.children = uniqBy(item.children.concat(payload.data.map(reItem => {
        reItem["uid"] = reItem.id
        reItem["pathName"] = item.pathName
        reItem["group"] = item.name
        reItem["deptId"] = item.id
        reItem["orgId"] = item.orgId
        reItem.id = `user-${reItem.id}`
        return reItem
      })).reverse(), 'id');
    }
  });
};
